<template>
  <Form
    ref="formRef"
    :form.sync="form"
    formTitle="查看"
    @closed="onClosed"
  />
</template>

<script>
import Form from './index.vue'
import useAdmateAdapter from '@/utils/useAdmateAdapter'
import ENUMS from '../enums'
import { mutations } from '../store'

let setupHasBeenCalled = false

export default {
  name: 'CommodityDetail',
  components: { Form },
  props: {
    show: {
      type: Boolean,
      required: true,
    },
    businessTypeId: {
      type: String,
      required: true,
    },
    id: {
      type: String,
      required: true,
    },
  },
  setup: props => {
    // fix: setup 执行了两次
    if (setupHasBeenCalled) {
      return
    } else {
      setupHasBeenCalled = true
    }

    let { id, businessTypeId } = props

    const {
      form,
      openForm,
    } = useAdmateAdapter({
      urlPrefix: ENUMS.ENUM_URL_PREFIX[businessTypeId],
      axiosConfig: ENUMS.ENUM_AXIOS_CONFIG[businessTypeId],
      getListProxy (getList, trigger) {
        // 不需要获取列表
      },
      form: {
        show: true,
        status: 'r',
        data: ENUMS.ENUM_DEFAULT_FORM_DATA[businessTypeId],
      },
    }, {
      getElFormRefOfFormData() {
        return this.$refs.formRef
      },
    })

    mutations.updateBusinessTypeId(businessTypeId)

    openForm.value({
      id,
      businessTypeId,
    })

    return {
      form,
      openForm,
    }
  },
  methods: {
    onClosed () {
      setupHasBeenCalled = false
      this.$emit('update:id', undefined)
      this.$emit('update:businessTypeId', undefined)
      this.$emit('update:show', false)
    }
  }
}
</script>

<style scoped>

</style>
